package com.srlg.batch.file;

import lombok.RequiredArgsConstructor;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.FileSystemResource;
import org.springframework.transaction.PlatformTransactionManager;

@Configuration
@RequiredArgsConstructor
public class DelFileJobConfig {

    private final JobRepository jobRepository;

    /**
     * @param deleteFilesInDir
     * @return
     */
    @Bean
    public Job delFileJob(Step deleteFilesInDir) {

        return new JobBuilder("Job", jobRepository)
                .start(deleteFilesInDir)
                .build();
    }

    /**
     * 删除文件 step
     *
     * @param transactionManager
     * @return
     */
    @Bean
    public Step deleteFilesInDir(PlatformTransactionManager transactionManager) {
        return new StepBuilder("deleteFilesInDir", jobRepository)
                .tasklet(fileDeletingTasklet(), transactionManager)
                .build();
    }

    /**
     * 删除文件 tasklet
     *
     * @return
     */
    @Bean
    public FileDeletingTasklet fileDeletingTasklet() {

        FileSystemResource fsr = new FileSystemResource("batch/test-dir");

        FileDeletingTasklet fileDeletingTasklet = new FileDeletingTasklet(fsr);

        return fileDeletingTasklet;
    }
}
